package cn.youruan.demo.controller;

import cn.youruan.demo.common.R;
import cn.youruan.demo.domain.Log;
import cn.youruan.demo.service.ILogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/log")
@Slf4j
public class LogController {
    @Autowired
    private ILogService logService;

    /**
     * 日志分页
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Log>> page(Integer page, Integer pageSize) {
        log.info("page = {},pageSize= {}",page,pageSize);
        //1.创建分页构造器
        Page<Log> pageInfo = new Page<>(page,pageSize);
        //2.判断条件
        LambdaQueryWrapper<Log> wrapper = new LambdaQueryWrapper<>();
        //3.执行查询
        wrapper.orderByDesc(Log::getLoginTime);
        logService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }

    /**
     * 删除日志
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public R<String> delete(Long id) {
        if (id !=null) {
            logService.removeById(id);
            return R.success("日志删除成功!!!");
        }
        return R.error("日志不存在!!!");
    }
}
